package com.scu.baseline.controller;

import com.scu.baseline.pojo.User;
import com.scu.baseline.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户表(User)表控制层
 *
 * @author lotswn
 * @since 2022-06-13 22:53:07
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户表(User)管理",tags = "用户表(User)管理接口API")
@ApiResponses({
        @ApiResponse(code=400,message="请求参数不完整或者错误"),
        @ApiResponse(code=404,message="找不到页面"),
        @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code=406,message="页面地址和接口名冲突"),
        @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code=500,message="后台服务逻辑错误")
})
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserServiceImpl userServiceImpl;

    /**
     * 查询用户分页数据
     * @param page  需要查询的页码
     * @param username  查询用户名
     * @param auth  查询权限级别
     * @param departmentid  查询部门编号
     * @return 单条数据
     */
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户分页数据的接口",notes = "查询用户分页数据的·接口",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "username",value = "用户名",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "auth",value = "权限级别",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "departmentid",value = "部门编号",paramType = "query",dataType = "String",required = false)
    })
    public Map<String, Object> selectByPage(@RequestParam(name = "page") int page, String username,String auth,String departmentid) {
        return this.userServiceImpl.selectByPage(page, username, auth, departmentid);
    }
    
    /**
     * 查询所有用户
     * @return 返回所有用户
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有用户的接口",notes = "查询所有用户的接口",httpMethod = "GET")
    public Map<String, Object> selectAll() {
        return this.userServiceImpl.selectAll();
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据主键删除一个用户的接口",notes = "根据主键删除一个用户的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> deleteById(@RequestParam("id") String id) {
        return this.userServiceImpl.deleteById(id);
    }

    /**
     * 通过主键删除多条数据
     *
     * @param ids 多个主键
     * @return map
     */
    @RequestMapping(value = "/batchDeleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据主键批量删除用户的接口",notes = "根据主键批量删除用户的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "多个主键",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> batchDeleteByIds(@RequestParam("ids") String ids) {
        String[] as = ids.split(",");
        System.out.println(as);
        for (int id = 0; id < as.length; id++) {
            this.userServiceImpl.deleteById(as[id]);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }

    /**
     * 用户账号登陆
     *
     * @param  username 用户名
     * @param  password 密码
     * @return 实例对象
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录接口",notes = "用户登录接口",httpMethod = "POST")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password) {
        return userServiceImpl.login(username, password);
    }

    /**
     * 获取当前登录用户详细信息
     *
     * @return 实例对象
     */
    @RequestMapping(value = "/getNowUser", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前用户的接口",notes = "获取当前用户的接口",httpMethod = "GET")
    public Object getNowUser() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return user;
    }


    /**
     * 通过ID查询单个用户
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ApiOperation(value = "通过主键查询单个用户的接口",notes = "通过主键查询单个用户的接口",httpMethod = "GET")
    public Map<String, Object> selectById(Integer id) {
        return this.userServiceImpl.selectById(id);
    }
    
    /**
     * 通过主键更新单个用户
     *
     * @param user 一个数据库对应的POJO数据对象
     * @return 单条数据
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ApiOperation(value = "通过主键更新单个用户的接口",notes = "通过主键更新单个用户的接口",httpMethod = "POST")
    public Map<String, Object> updateById(@RequestBody @ApiParam(name = "user",value = "用户pojo模型",required = true) User user) {
        return this.userServiceImpl.updateById(user);
    }

    
    /**
     * 通过一个pojo对象注册单条数据
     *
     * @param user user表实体类
     * @return 返回创建的信息
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "用户注册接口",notes = "前端仅提交username,password,userphone",httpMethod = "POST")
    public Map<String, Object> register(@RequestBody @ApiParam(name = "user",value = "用户pojo模型",required = true) User user) {
        return this.userServiceImpl.register(user);
    }

     /**
     * 客户端用户登录
     *
     * @param  username 用户名
     * @param  password 密码
     * @return 实例对象
     */
    @RequestMapping(value = "/clientlogin", method = RequestMethod.POST)
    @ApiOperation(value = "客户端用户登录接口",notes = "客户端用户登录接口",httpMethod = "POST")
    public Map<String, Object> clientlogin(@RequestParam String username, @RequestParam String password) {
        return userServiceImpl.clientlogin(username, password);
    }

//    public static void main(String[] args) {
//        String s = "150,150,204,200";
//        String[] as = s.split(",");
//        for (int i = 0; i < as.length; i++) {
//            System.out.println(as[i]);
//        }
//    }
}